/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2018
 */

package com.bm.console.action;

import java.util.List;

import com.bm.console.business.WebCityService;
import com.bm.console.pojo.WebCityItem;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bm.console.business.WebHousePriceRentTrendService;
import com.bm.console.pojo.WebHousePriceRentTrend;
import com.sys.object.common.ObjException;
import com.sys.object.common.ObjResultCode;
import com.sys.object.common.ObjResultException;
import com.sys.object.common.ObjResultJSON;
import com.sys.object.common.page.PageBean;

@Controller
public class WebHousePriceRentTrendAction {

    @Resource
    WebCityService webCityService;

    @Resource
    WebHousePriceRentTrendService webHousePriceRentTrendService;

    @RequestMapping(value = "/webHousePriceRentTrendList.do")
    public String webHousePriceRentTrendList(PageBean pageBean, Model model, WebHousePriceRentTrend webHousePriceRentTrend) {
        try {
            pageBean = webHousePriceRentTrendService.queryPageList(webHousePriceRentTrend, pageBean);
            model.addAttribute("pageBean", pageBean);
            model.addAttribute("startRow", pageBean.getStartRow());
            model.addAttribute("entity", webHousePriceRentTrend);

            List<WebCityItem> city_list = webCityService.selectAll();
            model.addAttribute("city_list", city_list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/web/webhousepricerenttrend/list";
    }

    /**
     * 新增动态 Get
     */
    @RequestMapping(value = "/addWebHousePriceRentTrend.do", method = RequestMethod.GET)
    public String addWebHousePriceRentTrend(@ModelAttribute("webHousePriceRentTrend") WebHousePriceRentTrend webHousePriceRentTrend, Model model, String id) {
        Integer prow = 0;
        try {
            List<WebCityItem> city_list = webCityService.selectAll();
            if (StringUtils.hasText(id)) {
                WebHousePriceRentTrend record = webHousePriceRentTrendService.queryByPrimaryKey(Integer.parseInt(id));
                model.addAttribute("entity", record);
            }
            model.addAttribute("city_list", city_list);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("prow", prow);
        return "/web/webhousepricerenttrend/add";
    }

    /**
     * 新增动态 Post
     */
    @RequestMapping(value = "/saveWebHousePriceRentTrend.do", method = RequestMethod.POST)
    public String saveWebHousePriceRentTrend(@Validated @ModelAttribute("webHousePriceRentTrend") WebHousePriceRentTrend webHousePriceRentTrend,
                                             BindingResult result, Model model, Integer prow,
                                             HttpServletRequest request) {
        ObjResultException<String> ret = new ObjResultException<String>();
        try {
            int success = webHousePriceRentTrendService.saveOrUpdate(webHousePriceRentTrend);
            if (success > 0) {
                ret.setResultCode(ObjResultCode.OK);
            } else {
                ret.setResultCode(ObjResultCode.USER_LOGIN_ERROR);
            }
        } catch (Exception e) {

            if (e.getClass().equals(ObjException.class)) {
                ret.setResultCode(((ObjException) e).getCode());
            } else {
                ret.setResultCode(ObjResultCode.ERROR);

            }
            e.printStackTrace();
            model.addAttribute("entity", webHousePriceRentTrend);
            model.addAttribute("ret", ret);
            return "/web/webhousepricerenttrend/add";
        }
        return "redirect:/webHousePriceRentTrendList.do?status=";
    }

    /**
     * 新增动态 Post
     */
    @RequestMapping(value = "/saveWebHousePriceRentTrendJson.do", method = RequestMethod.POST)
    @ResponseBody
    public ObjResultException<String> saveWebHousePriceRentTrendJson(@RequestBody WebHousePriceRentTrend webHousePriceRentTrend, Model model,
                                                                     HttpServletRequest request) {
        ObjResultException<String> ret = new ObjResultException<String>();
        try {
            int success = webHousePriceRentTrendService.saveOrUpdate(webHousePriceRentTrend);
            if (success > 0) {
                ret.setResultCode(ObjResultCode.OK);
            } else {
                ret.setResultCode(ObjResultCode.USER_LOGIN_ERROR);
            }
        } catch (Exception e) {
            if (e.getClass().equals(ObjException.class)) {
                ret.setResultCode(((ObjException) e).getCode());
            } else {
                ret.setResultCode(ObjResultCode.ERROR);
            }
        }
        return ret;
    }

    /**
     * 删除APP
     */
    @RequestMapping("/deleteWebHousePriceRentTrend.do")
    @ResponseBody
    public ObjResultException<String> deleteWebHousePriceRentTrend(Integer id) {
        ObjResultException<String> ret = new ObjResultException<String>();
        try {
            int success = webHousePriceRentTrendService.delete(id);

            if (success > 0) {
                ret.setResultCode(ObjResultCode.OK);
            } else {
                ret.setResultCode(ObjResultCode.USER_LOGIN_ERROR);
            }
        } catch (Exception e) {
            if (e.getClass().equals(ObjException.class)) {
                ret.setResultCode(((ObjException) e).getCode());
            } else {
                ret.setResultCode(ObjResultCode.ERROR);
            }
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * 修改状态APP
     */
    @RequestMapping("/updateStatusWebHousePriceRentTrend.do")
    @ResponseBody
    public ObjResultException<String> updateStatusWebHousePriceRentTrend(WebHousePriceRentTrend webHousePriceRentTrend) {
        ObjResultException<String> ret = new ObjResultException<String>();
        try {
            int success = webHousePriceRentTrendService.updateStatus(webHousePriceRentTrend);

            if (success > 0) {
                ret.setResultCode(ObjResultCode.OK);
            } else {
                ret.setResultCode(ObjResultCode.USER_LOGIN_ERROR);
            }
        } catch (Exception e) {
            if (e.getClass().equals(ObjException.class)) {
                ret.setResultCode(((ObjException) e).getCode());
            } else {
                ret.setResultCode(ObjResultCode.ERROR);
            }
        }
        return ret;
    }
}